package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSCharacter;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Nightmares18", "joku.rules", "Taha", "Fred"}, name="FrogCave", version=2.3D)
public class FrogCave extends Random
{
  private RSNPC frog;
  private boolean talkedToHerald;
  private boolean talkedToFrog;
  private int tries;

  public boolean activateCondition()
  {
    if (!this.game.isLoggedIn())
      return false;
    if (this.npcs.getNearest(new String[] { "Frog Herald" }) != null) if (this.objects.getNearest(new int[] { 5917 }) != null) {
        sleep(random(2000, 3000));
        if (this.npcs.getNearest(new String[] { "Frog Herald" }) != null);
        return this.objects.getNearest(new int[] { 5917 }) != null;
      }
    return false;
  }

  private RSNPC findFrog() {
    return this.npcs.getNearest(new Filter() {
      public boolean accept(RSNPC paramRSNPC) {
        return (!paramRSNPC.isMoving()) && (paramRSNPC.getHeight() == -278);
      } } );
  }

  private boolean canContinue() {
    return (this.interfaces.canContinue()) || (this.interfaces.getComponent(65, 6).isValid());
  }

  public int loop()
  {
    try {
      if (!activateCondition()) {
        this.talkedToHerald = false;
        this.frog = null;
        this.tries = 0;
        return -1;
      }
      Object localObject;
      if (canContinue())
      {
        if (!this.talkedToHerald) {
          localObject = this.interfaces.getComponent(242, 4);
          this.talkedToHerald = ((((RSComponent)localObject).isValid()) && ((((RSComponent)localObject).containsText("crown")) || (((RSComponent)localObject).containsText("is still waiting"))));
        }

        if (!this.interfaces.clickContinue()) {
          this.interfaces.getComponent(65, 6).doClick();
        }
        return random(600, 800);
      }
      if (getMyPlayer().isMoving()) {
        return random(600, 800);
      }
      if (!this.talkedToHerald) {
        localObject = this.npcs.getNearest(new String[] { "Frog Herald" });
        if (this.calc.distanceTo((RSCharacter)localObject) < 5) {
          if (!this.calc.tileOnScreen(((RSNPC)localObject).getLocation())) {
            this.camera.turnToCharacter((RSCharacter)localObject);
          }
          ((RSNPC)localObject).doAction("Talk-to");
          return random(500, 1000);
        }
        this.walking.walkTileMM(((RSNPC)localObject).getLocation());
        return random(500, 700);
      }

      if (this.frog == null) {
        this.frog = findFrog();
        if (this.frog != null) {
          log("Princess found! ID: " + this.frog.getID());
        }
      }
      if ((this.frog != null) && (this.frog.getLocation() != null) && ((!this.talkedToFrog) || (!canContinue())))
      {
        if (this.calc.distanceTo(this.frog) < 5) {
          if (!this.calc.tileOnScreen(this.frog.getLocation())) {
            this.camera.turnToCharacter(this.frog);
          }
          if (this.frog.doAction("Talk-to Frog")) {
            sleep(750, 1250);
            this.talkedToFrog = canContinue();
          }
          return random(900, 1000);
        }
        this.walking.walkTileMM(this.frog.getLocation());
        return random(500, 700);
      }

      this.tries += 1;
      if (this.tries > 200)
      {
        this.tries = 0;
        this.talkedToHerald = false;
      }
      return random(200, 400);
    }
    catch (Exception localException) {
      localException.printStackTrace();
    }
    return random(200, 400);
  }
}